// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Fk
import Fk.Widgets as W

ColumnLayout {
  id: root
  property var bgColor: "#314135"
  width: 20
  spacing: 0

  Repeater {
    id: rep
    model: ListModel {
      id: skills
    }
    OLLimitSkillItem {
      skillname: skillname_
      usedtimes: times
    }
  }

  ListModel {
    id: markList
  }

  Repeater {
    id: markRepeater
    model: markList

    Item {
      id: limitMarkItem
      width: 20
      height: 20
      Image {
        anchors.fill: parent
        fillMode: Image.PreserveAspectCrop
        source: SkinBank.getMarkPic(mark_name)

        MouseArea{ // 鼠标经过时显示文字，单击固定
          id: markArea
          anchors.fill: parent
          hoverEnabled: true
          enabled: mark_extra !== ""
          onEntered: {
            root.z = 5;
            limitMarkItem.z = 5;
            root.parent.z = 5;
            descriptionTip.visible = true;
          }
          onExited: {
            root.z = 0;
            limitMarkItem.z = 0;
            root.parent.z = 0;
            descriptionTip.visible = false;
          }
        }
        Rectangle {
          id: descriptionTip
          x: 0
          y: height - 10
          width: 300
          height: markDetailText.contentHeight
          color: "transparent"
          visible: false

          Text{
            x: 3
            width: parent.width
            id: markDetailText
            text: mark_extra
            font.pixelSize: 10
            wrapMode: Text.WordWrap
            textFormat: TextEdit.RichText
            color: "#fff7e7"
            font.family: "SimSun"
            style: Text.Outline
            styleColor: "#000000"
          }

          Rectangle { // same as cheatDrawer
            anchors.fill: parent
            anchors.margins: -5
            color: Qt.rgba(0.18, 0.17, 0.15, 0.8)
            radius: 5
            border.color: "#A6967A"
            border.width: 1
            z: -1
          }
        }
      }

      Text { // 右下角的文字，单个为翻译，1省略，数组为数量
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        text: special_value
        visible: special_value !== ""
        font.family: Config.libianName
        font.pixelSize: 12
        font.bold: true
        color: "white"
        style: Text.Outline
      }
    }
  }

  function update(skill, times) {
    for (let i = 0; i < rep.count; i++) {
      const data = skills.get(i);
      if (data.skillname_ === skill) {
        data.times = times;
        if (times == -1) {
          skills.remove(i);
        }
        return;
      }
    }
    if (times > -1) {
      skills.append({
        skillname_: skill,
        times: times,
      });
    }
  }

  function setMark(mark, data) {
    let i, modelItem;
    for (i = 0; i < markList.count; i++) {
      if (markList.get(i).mark_name === mark) {
        modelItem = markList.get(i);
        break;
      }
    }
    let special_value = '';

    if (data instanceof Array) {
      special_value += data.length;
      data = data.map((markText) => Lua.tr(markText)).join('<br>');
    } else {
      data = data === '1' ? '' : Lua.tr(data);
      special_value += data;
    }

    if (mark.startsWith('@!!')) { // @!! 追加翻译标记名和描述
      data = '<b>' + Lua.tr(mark) + '</b>'+ (data === '' ? '' : '×' + data) + '<br>' + Lua.tr(":" + mark);
    }

    if (modelItem) { // 如果已经存在
      modelItem.special_value = special_value;
      modelItem.mark_extra = data;
    } else {
      markList.append({ mark_name: mark, mark_extra: data, special_value }); // special_value 传数量， mark_extra 传内容（翻译后的）
    }
  }

  function removeMark(mark) {
    let i, modelItem;
    for (i = 0; i < markList.count; i++) {
      if (markList.get(i).mark_name === mark) {
        markList.remove(i, 1);
        return;
      }
    }
  }

}
